<?php

namespace addons\shopro\controller;

use addons\shopro\exception\Exception;
use think\Db;

class Goods extends Base
{

    protected $noNeedLogin = ['index', 'detail', 'lists', 'activity', 'seckillList', 'grouponList', 'store','recommended_lists','sales_enable'];
    protected $noNeedRight = ['*'];

    public function index()
    {
        // 测试腾讯云短信
        $user = \addons\shopro\model\User::get(10011);
        $walletLog = \addons\shopro\model\UserWalletLog::get(61);

        $user && $user->notify(
            new \addons\shopro\notifications\Wallet([
                'walletLog' => $walletLog,
                'event' => 'wallet_change'
            ])
        );
    }

    public function detail()
    {
        $id = $this->request->get('id');
        $groupon_id = $this->request->get('groupon_id/d');
        $detail = \addons\shopro\model\Goods::getGoodsDetail($id);
        
        // 记录足记
        \addons\shopro\model\UserView::addView($detail);

        $sku_price = $detail['sku_price'];      // 处理过的规格
        // tp bug json_encode 或者 toArray 的时候 sku_price 会重新查询数据库，导致被处理过的规格又还原回去了
        $detail = json_decode(json_encode($detail), true);
        $detail['sku_price'] = $sku_price;

        $detail['store_info'] = Db::name('shopro_store')->where('id',$detail['shopro_store_id'])->field('id,name,openhours,phone,province_name,city_name,area_name,address,latitude,longitude')->find();

        //开始-2022-2-12 获取浏览用户 积分抵扣金额
        //获取浏览用户
        $detail['user_view'] = Db::name('shopro_user_view')->alias('v')
                ->join('user u','u.id=v.user_id','left')
                ->field('v.id,v.user_id,v.goods_id,
                u.nickname,u.avatar,u.mobile')
                ->where(['v.goods_id'=>$detail['id']])
                ->order('v.updatetime desc,v.id desc')
                ->limit(6)->select();
        foreach ($detail['user_view'] as &$item){
            $item['avatar'] =  cdnurl($item['avatar'], true);
        }
        unset($item);

        //积分抵扣金额
        $shop_config = Db::name('shopro_config')->where(['name'=>'order','group'=>'basic'])->value('value');
        $shop_config = json_decode($shop_config,true);
        if ($shop_config['points_config']['enable'] == '1'){
            $detail['maximum_use_points_amount'] = $detail['maximum_use_points'] * $shop_config['points_config']['single_point_deduction'];
        }else{
            $detail['maximum_use_points_amount'] = 0;
        }

        //结束-2022-2-12 获取浏览用户 积分抵扣金额

        if ($groupon_id){
            $groupon_info = Db::name('shopro_activity_groupon')->where(['id'=>$groupon_id])->find();
            if (isset($groupon_info['expiretime'])){
                $groupon_info['expiretime'] = date('Y-m-d H:i:s',$groupon_info['expiretime']);
            }
            $detail['groupon_info'] = $groupon_info;
        }else{
            $detail['groupon_info'] = '';
        }



        //获取成团人数最低、最高   拼团有效期最低、最高
        $mix_user_num = empty(config('site.config_mix_user_num')) ? 2:intval(config('site.config_mix_user_num'));
        $max_user_num = empty(config('site.config_max_user_num')) ? 999:intval(config('site.config_max_user_num'));
        $mix_groupn_hour = empty(config('site.config_mix_groupn_hour')) ? 1:config('site.config_mix_groupn_hour');
        $max_groupn_hour = empty(config('site.config_max_groupn_hour')) ? 999:config('site.config_max_groupn_hour');
        $detail['mix_user_num'] = $mix_user_num;
        $detail['max_user_num'] = $max_user_num;
        $detail['mix_groupn_hour'] = $mix_groupn_hour;
        $detail['max_groupn_hour'] = $max_groupn_hour;


        $this->success('商品详情', $detail);
    }

    public function lists()
    {
        $params = $this->request->get();
        $data = \addons\shopro\model\Goods::getGoodsList($params);

        $this->success('商品列表', $data);

    }
    //推荐商品列表
    public function recommended_lists(){
        $params = $this->request->get();
        $data = \addons\shopro\model\Goods::getRecommendedGoodsList($params,true);

        $this->success('推荐商品列表', $data);
    }
    //商品销量是否显示
    public function sales_enable(){
        $shop_config = Db::name('shopro_config')->where(['name'=>'order','group'=>'basic'])->value('value');
        $shop_config = json_decode($shop_config,true);
       $this->success('获取成功',$shop_config['goods']);

    }

    /**
     * 获取商品支持的 自提点
     */
    public function store()
    {
        $params = $this->request->get();
        $data = \addons\shopro\model\Goods::getGoodsStore($params);

        $this->success('自提列表', $data);
    }


    // 秒杀列表
    public function seckillList() {
        $params = $this->request->get();

        $this->success('秒杀商品列表', \addons\shopro\model\Goods::getSeckillGoodsList($params));
    }


    // 拼团列表
    public function grouponList() {
        $params = $this->request->get();

        $this->success('拼团商品列表', \addons\shopro\model\Goods::getGrouponGoodsList($params));
    }


    public function activity()
    {
        $activity_id = $this->request->get('activity_id');
        $activity = \addons\shopro\model\Activity::get($activity_id);
        if (!$activity) {
            $this->error('活动不存在', null, -1);
        }
        
        $goods = \addons\shopro\model\Goods::getGoodsList(['goods_ids' => $activity->goods_ids]);
        $activity->goods = $goods;
        
        $this->success('活动列表', $activity);
    }

    public function favorite()
    {
        $params = $this->request->post();
        $result = \addons\shopro\model\UserFavorite::edit($params);
        $this->success($result ? '收藏成功' : '取消收藏', $result);
    }

    public function favoriteList()
    {
        $data = \addons\shopro\model\UserFavorite::getGoodsList();
        $this->success('商品收藏列表', $data);
    }


    public function viewDelete()
    {
        $params = $this->request->post();
        $result = \addons\shopro\model\UserView::del($params);
        $this->success('删除成功', $result);
    }


    public function viewList()
    {
        $data = \addons\shopro\model\UserView::getGoodsList();
        $this->success('商品浏览列表', $data);
    }



}
